import  {GoodsRequester} from '@/api/api.js'
const  goodsRequester = new GoodsRequester();
import router from "@/router";
import { ElMessage, ElMessageBox } from 'element-plus'

const state = { 
    //当前商品
    curGoods:{

    },
    //关键词
    keyword:" ",
    //判断是否处于搜索状态
    isSearch:false
}


const actions = {
    //获取拼租商品
    getGoods({commit},num=10){
        //更新搜索状态，防止再次清空
        if(state.isSearch)state.isSearch=false
        return goodsRequester.reqGoodsList(state.keyword,num)
    },
    //获取商品详细信息
    getMoreMsg({commit},msg){
        goodsRequester.reqGoodsMsg(msg.goodsId).then(res=>{
            let temp=msg
            console.log(res.data);
            temp.want=res.data.want
            temp.views=res.data.views
            commit("setCurGoods",temp)
        })
    },
    setLike({},operateObject,operateType=1){
        let id=0;
        if(operateObject==1||operateObject==2) id=state.curGoods.goodsId;
        else id=state.curGoods.userId;
        goodsRequester.reqMerchantLike(operateObject,operateType,id).then(()=>{
            let str=""
            if(operateObject==1){
                str="点赞成功"
            }
            if(operateObject==2){
                str="收藏成功"
            }
            if(operateObject==3){
                str="关注成功"
            }
            ElMessage({
                message: str,
                type: 'success',
            })
        })
    }
}

const mutations = {
    setCurGoods(state,goods){
        state.curGoods=goods
        router.push('/togetherDetail')
    },
    setKeyWord(state,keyword){
        //设置搜索状态，为True时第一次清空商品列表
        state.isSearch=true
        state.keyword=keyword
    },
}

const getters={
    
    
}

export default {
    namespaced:true,
    state,
    actions,
    mutations,
    getters
}